OU having a laugh?
By on 6 November, 2019
Overview
tl;dr When we have permission to modify an OU, we can modify its gpLink attribute in order to compromise any computer or user that belongs to that OU or its child OUs.
Before writing SharpGPOAbuse, I wanted to learn about how Group Policy Objects (GPOs) are processed and how all the different GPO components are linked together. While looking for potential attacks, one attribute that stood out immediately was gpLink. GpLink exists in all Active Directory containers such as the Domain object and Organizational Units (OUs) and includes the LDAP path of the GPOs that are linked to the container.
Although a full description of how GPOs work is beyond the purpose of this post, a few basic concepts are summarised below to make it easier for the reader to understand the steps of the attack.
GPOs consist of the following two components:
- Group Policy Container (GPC) - this is the LDAP part of the GPO and contains its attributes
- Group Policy Template (GPT) - this is located in Sysvol and contains the actual GPO settings
When a client (user or computer) processes the GPO settings, the following process takes place:
- The client retrieves the gpLink attribute of all containers in its directory structure. GpLink contains the LDAP path of the GPOs that must be applied.
- The client retrieves a number of attributes associated with each GPO. One of these attributes is gPCFileSysPath which contains the location of the actual GPO settings in Sysvol.
- The client establishes an SMB connection to the location pointed by gPCFileSysPath.
- The GPO settings are retrieved and applied by the client.
Of course, this is a simplified version of the actual process and only steps relevant to this attack are highlighted. Something worth remembering is that steps 1 and 2 take place over the LDAP protocol (port 389/tcp) and steps 3 and 4 over SMB (port 445/tcp).
What is this all about?
To perform the attack, the following requirements must be met:
- We need to have the necessary rights to edit the attributes of an OU.
- We must be able to add computer objects in the target domain.
- We must be able to add DNS records in the target domain.
The good news is that AD's default configuration allows any domain user to perform steps 2 and 3.
The following video demonstrates the various steps of the process for a successful attack:
So, how can I compromise users and computers?
The easiest way to explain the attack is by using an example. In this example, the target domain name is contoso.com. We have compromised bob.smith who is logged into the WRKSTN02 workstation and has the necessary permissions to modify the Finance OU. The victim user is alice.jones who is a member of the Finance OU:
![](/adobe/dynamicmedia/deliver/dm-aid--914ff849-df2d-4694-ade5-db4893440a8a/new-alice-ou.png?preferwebp=true&quality=82)
Bob is also a local administrator on WRKSTN02. Although this is not a (strict) requirement for the attack, it greatly facilitates it and simplifies the example.
![](/adobe/dynamicmedia/deliver/dm-aid--45906502-6a9f-4c6e-9118-30657fdce8bd/cs.png?preferwebp=true&quality=82)
Firstly, we need to add a new DNS A record for test.contoso.com that will point to the computer where bob.smith is logged in and we have our beacon (WRKSTN02). We can use Invoke-DNSUpdate from Powermad to do this:
Invoke-DNSUpdate -DNSType A -DNSName test -DNSData 10.1.1.22 -Realm contoso.com
![](/adobe/dynamicmedia/deliver/dm-aid--0d188429-e745-463f-b7f5-c5dbf560ad06/dns.png?preferwebp=true&quality=82)
Then, since we can modify the attributes of the Finance OU, we will change its gpLink attribute to point to something like the following:
[LDAP://cn={980F65E5-95F3-4536-81CF-1A48691F2D67},cn=policies,cn=system,DC=test,DC=contoso,DC=com;0]
To modify the attribute we can use the following PowerView functions:
Get-DomainObject Finance | Set-DomainObject -Set @{'GpLink'='[LDAP://cn={980F65E5-95F3-4536-81CF-1A48691F2D67},cn=policies,cn=system,DC=test,DC=contoso,DC=com;0]'}
![](/adobe/dynamicmedia/deliver/dm-aid--9fc806b7-c64e-4ceb-8c06-f51d29d1348b/new-powerview.png?preferwebp=true&quality=82)
This means that the client processing this gpLink attribute will initiate an LDAP connection to test.contoso.com and attempt to retrieve the GPO attributes (gPCFileSysPath, versionNumber, etc).
Since the client will initiate an LDAP connection (port 389/tcp) to test.contoso.com (10.1.1.22), we need to intercept the communication. To do this we can use Cobalt Strike's "rportfwd" command to open port 389 on the compromised host and forward it to a server under our control (10.2.2.40) outside the target network:
![](/adobe/dynamicmedia/deliver/dm-aid--476c9b10-3463-48a0-8d45-ac6f75ec1458/portfwd2.png?preferwebp=true&quality=82)
This server will be a Domain Controller for a dummy domain called test.contoso.com. Of course, there is no relationship between this dummy domain and the target contoso.com domain. Let's call this new computer ATLANTIC$. Since we control ATLANTIC$, we can run mimikatz to get its long machine password in cleartext. (First run the “Reset-ComputerMachinePassword” powershell command, then run sekurlsa::logonpasswords)
![](/adobe/dynamicmedia/deliver/dm-aid--02c49391-ebfd-4aee-a731-dbb3873b9be4/mimikatz1.png?preferwebp=true&quality=82)
Now that we have the machine password for the dummy DC, we can add a new computer object in the original contoso.com domain and configure it with that same password. The new computer object will have the name TEST. We can use Powermad to do this (remember to use some proper escaping for the long password string):
$pass = "cP:Wbp8!Faz-2`$06CXvM\*m`$U)+[a/VBA2>60o8`"Ri1 bIMwxN,j8AWzFENk7)`$R;CSYG1%;UIi``8,]4PY`"G``hc<'k63KroS(^rT9%BeI'V35lMN9YN7Pgh;"
New-MachineAccount -MachineAccount test -Password $(ConvertTo-SecureString $pass -AsPlainText -Force)
![](/adobe/dynamicmedia/deliver/dm-aid--9e958f56-ce36-41a7-bf93-2ecd7461e9d1/new-machine.png?preferwebp=true&quality=82)
We also need to make sure that the following SPNs are registered to the new computer object in order for the Kerberos authentication to work when the client attempts to make an LDAP connection to our dummy test.contoso.com domain (I used a slightly modified version of Powermad to register these SPNs when the object was created).
LDAP/test.contoso.com
LDAP/TEST
HOST/TEST.contoso.com
HOST/TEST
Now, when the client retrieves the value of the modified gpLink attribute, it will ask for a TGS to LDAP/test.contoso.com which will be encrypted with the hash of the password for the ATLANTIC$ computer we provided earlier. Therefore, when our dummy domain controller receives the TGS, it will be able to decrypt it successfully.
So far we managed to force the client to establish an LDAP connection to a rogue DC under our control. Following the GPO update process we discussed in the beginning of this post, the client will now request a number of the GPO's attributes, including gPCFileSysPath. Therefore, we need to create a malicious GPO (we will call it TestGPO) in our dummy test.contoso.com domain, which will contain any settings we want to push to the victims.
![](/adobe/dynamicmedia/deliver/dm-aid--f2011a3e-0cb0-4ce4-9e68-b05f3c4e592a/screenshot2.png?preferwebp=true&quality=82)
Important note: The GUID of the GPO must be included in the LDAP path we placed earlier in the modified gpLink attribute of the Finance OU.
In this example attack, a simple immediate task to execute calc.exe was configured.
![](/adobe/dynamicmedia/deliver/dm-aid--5f1ba09c-6a6d-4a19-9662-360eb1e5ffbf/new-task2.png?preferwebp=true&quality=82)
Now, the client will connect over SMB to where the gPCFileSysPath attribute points in order to retrieve the GPO settings. This is where our local admin rights on WRKSTN02 are useful. We can use these rights to create a new shared folder called Sysvol and allow members of the Authenticated Users group to read its contents:
![](/adobe/dynamicmedia/deliver/dm-aid--e37c41ff-2173-4f8d-b67e-098c2b315c48/sysvol.png?preferwebp=true&quality=82)
This shared folder will host the malicious GPO settings which we can copy from the Sysvol share of our "fake" domain controller for test.contoso.com.
Also, the existing value of the gPCFileSysPath attribute of the TestGPO within the test.contoso.com domain must be replaced with the following:
\\wrkstn02.contoso.com\SysVol\contoso.com\Policies\{980F65E5-95F3-4536-81CF-1A48691F2D67}
When the client retrieves the value of the gPCFileSysPath attribute, they will know that the GPO settings for the TestGPO are located in the Sysvol share of the WRKSTN02 workstation.
The following screenshot shows that at the next GPO refresh cycle, the immediate task executing calculator was created for alice.jones, who is a member of the Finance OU, and our attack was successful:
![](/adobe/dynamicmedia/deliver/dm-aid--c63a9668-5f17-4092-90ae-f4193971673f/screen-shot-2019-09-11-at-6-36-50-pm.png?preferwebp=true&quality=82)
Essentially, by following the process above we used our rogue DC to host the LDAP part of the malicious GPO (GPC) and we used the compromised workstation to host the actual settings of the malicious GPO in a share called Sysvol (GPT).
The last thing I need to note is that this attack was possible even when hardened UNC paths were configured. Since we are not performing any man-in-the-middle attacks and our kerberos authentication is working as expected this security control cannot be used to prevent this technique. The following screenshot demonstrates the hardened UNC paths that were configured during this attack:
![](/adobe/dynamicmedia/deliver/dm-aid--07e86ea4-47bd-4dd0-9fbd-c80bd8f6a70a/unc-path3.png?preferwebp=true&quality=82)
As can be seen in the following capture from Wireshark, the SMB traffic between the victim WRKSTN01 (10.1.1.20) and the compromised WRKSTN02 (10.1.1.22) was encrypted, implying that the hardened UNC paths configuration was enforced:
![](/adobe/dynamicmedia/deliver/dm-aid--d6c4292c-b17f-449b-9803-36499d4468d8/wireshark.png?preferwebp=true&quality=82)
The attack described in this post was reported to Microsoft and the official response was that this was by design and that they would not perform any further action.